﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace PerformanceAssessment.Models
{
    /// <summary>
    /// Assessment period entity
    /// </summary>
    public class AssessmentPeriod
    {
        public int ID { get; set; }
        [Required]
        public string Description { get; set; }
        [Required]
        [DateStart]
        [Display(Name="Start Date")]
        public DateTime FromDate { get; set; }
        [Required]
        [DateEnd(DateStartProperty="FromDate", ErrorMessage="End date must be greater than Start date")]
        [Display(Name="End Date")]
        public DateTime ToDate { get; set; }
        [ForeignKey("PreviousAssessmentPeriod")]
        [Display(Name="Previous Assessment Period")]
        public int? IdPreviousAssessmentPeriod { get; set; }

        public virtual AssessmentPeriod PreviousAssessmentPeriod { get; set; }
        public virtual List<Assessment> Assessments { get; set; }
        
        public virtual List<Team> Teams { get; set; }

        public EmailUtil GetAutoevaluationOpenEmail()
        {
            EmailUtil email = new EmailUtil();
            foreach (Team t in this.Teams)
            {
                foreach (Employee employee in t.Employees)
                {
                    email.To.Add(employee.Email);
                }
            }

            email.Subject = string.Format("Evaluation Period Opened - {0}", this.Description);
            email.Body = string.Format("<p>The evaluation Period <strong>{0}</strong> that covers the dates from <strong>{1}</strong> to <strong>{2}</strong> is now available. Please access the Performance Assessment application and complete this evaluation.</p><br /><a href=\"{3}/Assessments/Index?assessmentPeriods={4}\">Go to assessment!</a>",
                this.Description,
                this.FromDate.ToShortDateString(),
                this.ToDate.ToShortDateString(),
                HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority),
                this.ID);
            email.IsBodyHtml = true;
            return email;
        }
    }
}